package com.ruoyi.player.controller;

import com.ruoyi.player.handler.NonStaticResourceHttpRequestHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
 * @Description
 * @Author Vzn
 * @Date 2021/5/20 20:40
 */
@RestController
public class PlayerController {
    @Autowired
    private NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;
    @GetMapping("/getPlayResource/{resourceId}")
    public void getPlayResource(@PathVariable("resourceId") Long resourceId,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        Resource resource = new FileSystemResource("E:\\ChromeDownload\\985187c2a20180497bb78087a8c27595.mp3");

        if (resource != null) {
            Path path = Paths.get("D:\\chromeDownload\\11827.mp3");
            if (Files.exists(path)) {
                String mimeType = Files.probeContentType(path);
                if (!StringUtils.isEmpty(mimeType)) {
                    response.setContentType(mimeType);
                }
                request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, path);
                nonStaticResourceHttpRequestHandler.handleRequest(request, response);
            } else {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
            }
        }
    }
}